package com.ruoyi.system.controller;
import java.util.Date;
import java.util.HashMap;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import com.ruoyi.service.SnowflakeIdGenerator;
import com.ruoyi.system.domain.T3rdstock;
import com.ruoyi.system.service.IT3rdstockService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TGoods;
import com.ruoyi.system.service.ITGoodsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 商品Controller
 * 
 * @author ruoyi
 * @date 2024-05-15
 */
@RestController
@RequestMapping("/supplier/goodsManage")
public class TGoodsController extends BaseController
{
    @Autowired
    private ITGoodsService tGoodsService;

    @Autowired
    private SnowflakeIdGenerator snowflakeIdGenerator;

    @Autowired
    private IT3rdstockService t3rdstockService;

    /**
     * 查询商品列表
     */
    @GetMapping("/list")
    public TableDataInfo list(TGoods tGoods)
    {
        startPage();
        List<TGoods> list = tGoodsService.selectTGoodsList(tGoods);
        for (TGoods goods : list) {
            T3rdstock t3rdstock = new T3rdstock();
            t3rdstock.setGoodsId(goods.getGoodsId());
            T3rdstock res = t3rdstockService.selectGoodsStockFromT3rdstock(t3rdstock);
            if (res != null) goods.setGoodsStock(res.getSupplierStock() + goods.getSupplierStock());
            else goods.setGoodsStock(goods.getSupplierStock());
        }

        return getDataTable(list);
    }


    /**
     * 查询商品列表
     */
    @GetMapping("/listSelect")
    public AjaxResult listSelect(TGoods tGoods)
    {
        return success(tGoodsService.selectTGoodsList(tGoods));
    }

    /**
     * 查询商品列表
     */
    @GetMapping("/listBySupplier")
    public TableDataInfo listBySupplier(TGoods tGoods, String supplierId)
    {
        startPage();
        List<TGoods> list = tGoodsService.selectTGoodsList(tGoods);
        for (TGoods goods : list) {
            T3rdstock t3rdstock = new T3rdstock();
            t3rdstock.setGoodsId(goods.getGoodsId());
            T3rdstock res = t3rdstockService.selectGoodsStockFromT3rdstock(t3rdstock);
            if (res != null) goods.setGoodsStock(res.getSupplierStock() + goods.getSupplierStock());
            else goods.setGoodsStock(goods.getSupplierStock());
        }
        return getDataTable(list);
    }

    /**
     * 导出商品列表
     */
    @Log(title = "商品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TGoods tGoods)
    {
        List<TGoods> list = tGoodsService.selectTGoodsList(tGoods);
        ExcelUtil<TGoods> util = new ExcelUtil<TGoods>(TGoods.class);
        util.exportExcel(response, list, "商品数据");
    }

    /**
     * 获取商品详细信息
     */
    @GetMapping(value = "/{goodsId}")
    public AjaxResult getInfo(@PathVariable("goodsId") String goodsId)
    {
        return success(tGoodsService.selectTGoodsByGoodsId(goodsId));
    }
    @GetMapping(value = "/info/{goodsId}")
    public AjaxResult getDetailedInfo(@PathVariable("goodsId") String goodsId)
    {
        return success(tGoodsService.selectTGoodsByGoodsId(goodsId));
    }

    /**
     * 新增商品
     */
    @Log(title = "商品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TGoods tGoods)
    {
        tGoods.setGoodsId(String.valueOf(snowflakeIdGenerator.nextId()));
        return toAjax(tGoodsService.insertTGoods(tGoods));
    }

    /**
     * 修改商品
     */
    @Log(title = "商品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TGoods tGoods)
    {
        return toAjax(tGoodsService.updateTGoods(tGoods));
    }

    /**
     * 删除商品
     */
    @Log(title = "商品", businessType = BusinessType.DELETE)
	@DeleteMapping("/{goodsIds}")
    public AjaxResult remove(@PathVariable String[] goodsIds)
    {
        for (String s :goodsIds) {
            TGoods tGoods = tGoodsService.selectTGoodsByGoodsId(s);
            tGoods.setStatus("2");
            int res = tGoodsService.updateTGoods(tGoods);
            if (res == 0) return error();
        }

        return success("删除成功");
    }

    /**
     * 图片上传
     */
    @Log(title = "商品图片", businessType = BusinessType.UPDATE)
    @PostMapping("/avatar")
    public AjaxResult avatar(@RequestParam("avatarfile") MultipartFile file, String goodsId) throws Exception
    {
        if (!file.isEmpty())
        {
            String avatar = FileUploadUtils.upload(RuoYiConfig.getAvatarPath(), file, MimeTypeUtils.IMAGE_EXTENSION);
            TGoods tGoods = new TGoods();
            tGoods.setGoodsId(goodsId);
            tGoods.setGoodsPicture(avatar);
            if (tGoodsService.updateTGoods(tGoods) == 1)
            {
                AjaxResult ajax = AjaxResult.success();
                ajax.put("imgUrl", avatar);
                return ajax;
            }
        }
        return error("上传图片异常，请联系管理员");
    }
}
